﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Company.StudyClub.Model;
using Company.StudyClub.BLL.Cookie;
using Company.StudyClub.BLL.User;
using Company.StudyClub.BLL.Site;

/// <summary>
///PageBase 的摘要说明
/// </summary>
public class PageBase : System.Web.UI.Page
{
    public PageBase()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    // Current User
    private User_Info currentUser = new User_Info();
    public User_Info CurrentUser
    {
        get
        {
            if (this.currentUser.UserID == 0)
            {
                try
                {
                    string seUser = CookieManager.ReadCookie(UserConst.COOKIE_SERIALIZE_NAME).Trim();
                    try
                    {
                        UserBLL objBLL = new UserBLL();
                        this.currentUser = objBLL.DeSerialUser(seUser);
                    }
                    catch { }
                }
                catch { }
            }
            return this.currentUser;
        }
    }

    //Current Enviroment
    private Site_Enviroment currentEnvironment;
    public Site_Enviroment CurrentEnvironment
    {
        get
        {
            if (currentEnvironment == null)
            {
                currentEnvironment = new Site_Enviroment();
                currentEnvironment.Area = AreaLayer.Instance.GetAreaName();

                try { currentEnvironment.ServerName = ConfigurationManager.AppSettings.Get("ServerName"); }
                catch { }
                try
                {
                    if (CookieManager.ReadCookie("DevModeCookie") != null && CookieManager.ReadCookie("DevModeCookie").Length > 0)
                        currentEnvironment.DevMode = Boolean.Parse(CookieManager.ReadCookie("DevModeCookie"));
                }
                catch { }
                try
                {
                    if (CookieManager.ReadCookie("LanguageCookie") != null && CookieManager.ReadCookie("LanguageCookie").Length > 0)
                        currentEnvironment.Language = CookieManager.ReadCookie("LanguageCookie");
                }
                catch { }
                try
                {
                    if (CookieManager.ReadCookie("CurrentFunctionID") != null && CookieManager.ReadCookie("CurrentFunctionID").Length > 0)
                        currentEnvironment.FunctionID = int.Parse(CookieManager.ReadCookie("CurrentFunctionID"));
                }
                catch { }
            }
            return this.currentEnvironment;
        }
    }

    // Path
    public string ThemePath(string imagePath)
    {
        return this.ResolveClientUrl("~/Images/" + imagePath);
    }

    public string FilePath(string filePath)
    {
        return this.ResolveClientUrl("~/" + filePath);
    }

    // URL Parameters   
    public string GetString(string name)
    {
        string rtvl = "";
        try
        {
            rtvl = Request[name].ToString().Trim();
        }
        catch { }
        return rtvl;
    }
    public int GetInt(string name)
    {
        int id = 0;
        try
        {
            id = Int32.Parse(Request[name].ToString().Trim());
        }
        catch { }
        return id;
    }
}
